﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 应用程序域动态库１
{
    /// <summary>
    /// 用于传送应用程序域的对像
    /// </summary>
    public class DemoClass : System.MarshalByRefObject
    {

        public void SetThis()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.SetData("demo", this);

        }

        private Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
        /// <summary>
        /// 要实现在默认AppDomain中实现对副AppDomain的导入控制，必须获取副AppDomain中的RemotLoader的实例如下图
        /// </summary>
        /// <param name="fullPath"></param>
        public void Load(string fullPath)
        {
            byte[] fsContent;
            using (FileStream fs = File.OpenRead(fullPath))
            {
                fsContent = new byte[fs.Length];
                fs.Read(fsContent, 0, fsContent.Length);
            }
            Assembly assembly = Assembly.Load(fsContent);
            assemblies.Add(assembly.GetName().Name, assembly);
        }

        System.Collections.Concurrent.ConcurrentQueue<string> stsqueue = new System.Collections.Concurrent.ConcurrentQueue<string>();

        List<string> ccc = new List<string>();
        /// <summary>
        /// 集合
        /// </summary>
        public List<string> Ccc
        {
            get { return ccc; }
            set
            {
                ccc = value;
                foreach (var item in value)
                {
                    stsqueue.Enqueue(item);
                }

            }
        }

        public Guid GetGuid { get { return Guid.NewGuid(); } }

        private DemoClass _Obj = null;

        public DemoClass Obj
        {
            get { return _Obj; }
            set { _Obj = value;

            AppDomain currentDomain = AppDomain.CurrentDomain;
             
            Console.WriteLine("设置新对像域{0},Guid{1}", currentDomain.FriendlyName,Obj.GetGuid);
            

            }
        }

        public void 引起一个末处理的异常()
        {
            Console.WriteLine("当前应用程序域对像{0}", System.Threading.Thread.GetDomain().FriendlyName);

            
            //引起一个异常
            throw new NotFiniteNumberException();
        }

        



        ProxyAppDomain dom = new ProxyAppDomain();
        public void 加入自己的异常处理()
        {
            dom.CallBack();
        }
    }
}
